package com.kong.controller;

import com.kong.common.core.controller.BaseController;
import com.kong.common.core.domain.AjaxResult;
import com.kong.common.core.page.TableDataInfo;
import com.kong.common.utils.SecurityUtils;
import com.kong.entity.dto.QueryAttendanceRecordsDto;
import com.kong.entity.pojo.AttendanceRecords;
import com.kong.entity.vo.AitVo;
import com.kong.service.impl.AitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.kong.common.utils.PageUtils.startPage;

/**
 * 加油
 *
 * @author konglingfeng
 * @date 2025/11/1
 * @desc
 */
@RestController
@RequestMapping("/ait")
public class AitController extends BaseController {

    @Autowired
    private AitService aitService;

    /**
     * 查询打卡信息列表
     *
     * @return 结果
     */
    // @PreAuthorize("@ss.hasPermi('system:records:list')")
    @GetMapping("/list")
    public TableDataInfo list(QueryAttendanceRecordsDto queryAttendanceRecordsDto)
    {
        startPage();
        List<AitVo> list = aitService.selectAttendanceRecordsList(queryAttendanceRecordsDto);
        System.out.println("list = " + list);
        return getDataTable(list);
    }

    /**
     * 上班打卡
     *
     * @return 结果
     */
    // @PreAuthorize("@ss.hasPermi('system:records:morningDak')")
    @PostMapping("/morningDak")
    public AjaxResult morningDak()
    {
        Date date = new Date();
        // 检查是否已打卡
        if (aitService.checkAttendanceRecordsExist(SecurityUtils.getUserId(), date))
        {
            return AjaxResult.error("您已打过上班卡，无需重复操作");
        };
        aitService.insertAttendanceRecords();
        if (aitService.isLate(date)){
            aitService.updateAttendanceRecordsStatus(SecurityUtils.getUserId(), date, 2);
        }
        return AjaxResult.success("上班打卡成功");
    }

     /**
      * 下班打卡
      *
      * @return 结果
      */
    // @PreAuthorize("@ss.hasPermi('system:records:afternoonDak')")
    @PostMapping("/afternoonDak")
    public AjaxResult afternoonDak()
    {
        Date date = new Date();
        // 检查是否已打卡
        if (!aitService.checkAttendanceRecordsExist(SecurityUtils.getUserId(), date))
        {
            return AjaxResult.error("您未打过上班卡，无需下班打卡");
        };
        aitService.insertAttendanceAfternoonPunch(SecurityUtils.getUserId(), date);
        if (aitService.isLeaveEarly(date)){
            aitService.updateAttendanceRecordsStatus(SecurityUtils.getUserId(), date, 3);
        }
        return AjaxResult.success("下班打卡成功");
    }

    /**
     * 获取本月考勤统计信息
     */
    @GetMapping("/statistics")
    public AjaxResult getCurrentMonthStatistics() {
        Map<String, Object> map = aitService.getCurrentMonthAttendanceStatistics();
        System.out.println("map = " + map);

        return AjaxResult.success(map);
    }

}
